package main

import "log"

func main() {
	log.Println(threeSum([]int{-1, 0, 1, 2, -1, -4}))
}

// 找出数组中三个数相加等于0，数组三元组列表
//给定数组 nums = [-1, 0, 1, 2, -1, -4]，
//
//满足要求的三元组集合为：
//[
//[-1, 0, 1],
//[-1, -1, 2]
//]
func threeSum(nums []int) [][]int {
	k := make(map[int]int)
	m := make(map[int]bool)
	var results [][]int
	for _, x := range nums {
		for _, y := range nums {
			if _, ok := k[-x-y]; ok {
				if x == y && x == 0 {
					continue
				}
				if _, ok := m[-x*x*y-x*y*y]; !ok {
					result := []int{y, x, -x - y}
					results = append(results, result)
					m[-x*x*y-x*y*y] = true
				}
				break
			} else {
				k[y] = 1
			}
		}
	}
	return results
}
